package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.client.po.ProjectPo;
import com.msh.artascope.devcloud.client.qo.ProjectQo;
import com.msh.artascope.devcloud.service.dao.ProductDao;
import com.msh.artascope.devcloud.service.model.query.ProductQueryVo;
import com.msh.artascope.devcloud.service.model.vo.ProductVo;
import com.msh.artascope.devcloud.service.service.ProjectService;
import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.client.exception.BizException;
import javax.annotation.Resource;

import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.ProductPo;
import com.msh.artascope.devcloud.client.qo.ProductQo;
import com.msh.artascope.devcloud.service.service.ProductService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.util.CollectionUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class ProductServiceImpl extends TenantBaseServiceImpl<ProductPo,ProductQo> implements ProductService {

  @Resource
  private ProductDao productDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Resource
  private ProjectService projectService;

  @Override
  public long insert(ProductPo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<ProductPo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }


  @Override
  public int delete(Long id, Long tenantId) {
    ProjectPo projectPo = projectService
        .listOne(new ProjectQo().setProductId(id).setTenantId(tenantId));
    BizException.doThrowIfTrue("请先删除所有项目", null != projectPo);
    return super.delete(id, tenantId);
  }


// ----------前端vo处理-------------------------------------------------------

  private ProductVo toVo(ProductPo po){
    ProductVo vo = po.convert(ProductVo.class);
    return vo;
  }

  private List<ProductVo> toVoList(List<ProductPo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public ProductVo getVo(Long id, Long tenantId) {
    ProductPo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    ProductVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<ProductVo> listVo(ProductQueryVo q){
    ProductQo qo = q.convert(ProductQo.class);
    List<ProductPo> poList = this.list(qo);
    List<ProductVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<ProductVo>> pageVo(ProductQueryVo param) {
    ProductQo qo = param.convert(ProductQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<ProductVo>emptyList()).setCount(count);
    }
    List<ProductPo> poList = this.list(qo);
    List<ProductVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
